#ifndef CVX_MARKER_H
#define CVX_MARKER_H

#include <vector>
#include "cvxImage.h"


struct MarkerCriteria
{
	MarkerCriteria()
	{
		_thresholdParameter = 7.0;
		_minPixels = 100;
		_maxPixels = 560;
		_refineCorner = true;
		_fastCheck    = true;
	}
	float _thresholdParameter;
	int   _minPixels;
	int   _maxPixels;
	bool  _refineCorner;
	bool  _fastCheck;
};


enum { CVX_MARKER_SUCCESS=1, CVX_MARKER_LESS_NUMBER=2, CVX_MARKER_DIF_NUMBER=3, CVX_MARKER_CLOSE_MARGIN=4};

class CvxMarker
{
public:
	CvxMarker();
	~CvxMarker();
	void GetCenterPts(void);
	bool GetSubArea(int dilateLength, int w, int h);
	//AddOffset is used in marker tracking, in sub images
	void AddOffset(int x, int y);

	friend bool operator<(const CvxMarker &m1,const CvxMarker&m2)
	{
		if (m1.m_id == m2.m_id)
		{
			return m1.m_perimeter > m2.m_perimeter;
		}
		else
		{
			return m1.m_id < m2.m_id;
		}		
	}

private:
	void ComputerPerimeter(void);
	float GetPerimeter(void);

public:
	int m_id;
	vector<Point2f> m_corners;
	Point2f m_centerPt;
	float m_perimeter;
	CvRect m_subArea;
public:
	//detect marker in an image
	static int detect(IplImage *grayImage, vector<CvxMarker> &detectedMarkers, 
					  int markerNumber, const MarkerCriteria &criteria);

	//tracking marker position in consequence frames
	static bool tracking(IplImage *grayImage, vector<CvxMarker> &detectedMarkers,
						 int markerNumbers, const MarkerCriteria &criteria, 
						 const vector<CvxMarker> &predictedMarkers);

	//create marker image, id >=0 && id < 1024
	static cv::Mat createMarkerImage(int id, int size);

};


#endif